#! /bin/sh


#####################################
# READ PARAMS
#
app=$1
arch=$2

echo "*** SCRIPT install_vcomponents_v4rev ***"
echo "arch = $arch"


#####################################
# PREPARE variables:

# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
if [ "$arch" = "64" ] ; then
	sys_vcomp=/usr/local/lib/vcomponents_x64
	dllsuff="_x64"
else	
	sys_vcomp=/usr/local/lib/vcomponents
	dllsuff=""
fi

app_vcomp="$app"/Contents/vcomponents$dllsuff
exec_vcomp="@executable_path/../vcomponents$dllsuff"


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvshared_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvkernel_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvclient_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvreport_fat_release$dllsuff.dylib 	"$app_vcomp"
fi


####################################
# Copy Folders:
####################################

# vresources
cp -R $sys_vcomp/vresources 					"$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvshared_fat_release$dllsuff.dylib

	# change id of VKERNEL
	install_name_tool -id "$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib"   "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/VSDK.framework/Versions/A/VSDK" "$exec_vcomp/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	# change id of VCLIENT
	install_name_tool -id "$exec_vcomp/libvclient_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib

	# # change id of VREPORT
	install_name_tool -id "$exec_vcomp/libvreport_fat_release$dllsuff.dylib" "$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" "$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" "$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib

    
    
#####################################
# now we need to update yet APP/Contents/MacOS/Externals/v4rev.bundle/Contents/MacOS/v4rev
#####################################
app_v4rev="$app"/Contents/MacOS/Externals/v4rev.bundle/Contents/MacOS/v4rev$dllsuff

if [ -e "$app_v4rev" ] ; then
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 		"$app_v4rev"
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 		"$app_v4rev"
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 		"$app_v4rev"
	install_name_tool -change "$sys_vcomp/libvreport_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvreport_fat_release$dllsuff.dylib" 		"$app_v4rev"
fi

#####################################
# now we need to update yet APP/Contents/MacOS/Externals/dbvalentina/Contents/MacOS/dbvalentina
#####################################
app_dbvalentina="$app"/Contents/MacOS/Externals/database_drivers/dbvalentina/Contents/MacOS/dbvalentina$dllsuff

if [ -e "$app_dbvalentina" ] ; then
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
	install_name_tool -change "$sys_vcomp/libvreport_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvreport_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
fi


#####################################
# now we need to update yet APP/Contents/MacOS/Externals/dbvsqlite/Contents/MacOS/dbvsqlite
#####################################
app_dbvsqlite="$app"/Contents/MacOS/Externals/database_drivers/dbvsqlite/Contents/MacOS/dbvsqlite$dllsuff

if [ -e "$app_dbvsqlite" ] ; then
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 		"$app_dbvsqlite"
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 		"$app_dbvsqlite"
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 		"$app_dbvsqlite"
	install_name_tool -change "$sys_vcomp/libvreport_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvreport_fat_release$dllsuff.dylib" 		"$app_dbvsqlite"
fi
